package cc.rengu.igas.channel.cups.core.service.trans;

import cc.rengu.igas.channel.cups.common.constant.CupsConfigConstant;
import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.common.constant.CupsTreeNodeConstant;
import cc.rengu.igas.channel.cups.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.cups.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.cups.core.model.CupsFieldRspBean;
import cc.rengu.igas.channel.cups.core.service.base.BaseManageService;
import cc.rengu.igas.channel.cups.facade.request.CupsLineTestRequest;
import cc.rengu.igas.channel.cups.facade.response.CupsLineTestResponse;
import cc.rengu.igas.share.facade.bean.CupsField;
import cc.rengu.igas.share.facade.bean.CupsFieldBean;
import cc.rengu.jradp.schedule.ScheduleImpl;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;

/**
 * cups线路测试
 *
 * @author yangfan
 * @version 1.0
 * @date 2020/11/18
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public class CupsLineTestService extends BaseManageService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        CupsLineTestRequest cupsLineTestRequest = new CupsLineTestRequest();
        ConvertUtil.convertOutput(cupsLineTestRequest);
        return cupsLineTestRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        CupsLineTestRequest cupsLineTestRequest = (CupsLineTestRequest) request;
        return directCups(cupsLineTestRequest);
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        CupsLineTestResponse cupsLineTestResponse = (CupsLineTestResponse) response.getResult();
        ConvertUtil.convertInput(cupsLineTestResponse);
    }

    /**
     * 直连cups
     *
     * @param cupsLineTestRequest 线路测试请求体
     * @return ChnlBizResponse
     * @throws Exception 失败
     */
    ChnlBizResponse directCups(CupsLineTestRequest cupsLineTestRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        CupsLineTestResponse cupsLineTestResponse = new CupsLineTestResponse();
        SysParamService sysParamService = new SysParamServiceImpl();
        String instId = cupsLineTestRequest.getHeader().getInstId();
        SysParam sysParam = sysParamService.getSysParamInfo(instId, CupsConfigConstant.CUPS_CONF_NODE, CupsConfigConstant.CUPS_FWD_INS_CODE);
        if (null == sysParam || StringUtil.isEmptyOrNull(sysParam.getParamValue())) {
            rglog.error("未获取到<{}>机构的发送机构标识", instId);
            throw new BizException(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespCode(), OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespDesc());
        }
        String fwdInstCode = sysParam.getParamValue();
        String traceNo = cupsLineTestRequest.getHeader().getTraceNo();
        /* 流水号规则： */
        String transDateTime = cupsLineTestRequest.getHeader().getTransDate().substring(4, 8) +
                cupsLineTestRequest.getHeader().getTransTime();
        String cupsSsn = cupsLineTestRequest.getHeader().getTransTime();
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(CupsTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);

        CupsField cupsField = new CupsField();
        BeanUtil.beanCopy(cupsLineTestRequest, cupsField);
        cupsField.setMsgType("0820");
        cupsField.setTransDateTime(transDateTime);
        cupsField.setCupsTraceNo(cupsSsn);
        cupsField.setFwdInstCode(fwdInstCode);
        cupsField.setManageNetCode("301");
        CupsFieldBean cupsFieldBean = new CupsFieldBean();
        cupsFieldBean.setCupsField(cupsField);
        ConvertUtil.convertInput(cupsFieldBean);

        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, cupsLineTestRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        String tunlSsn = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.CHANNEL_SEQ_NUM);
        UnifiedCache.remove(CupsParamConstant.CACHE_CUPS_SN, tunlSsn);
        if (ScheduleImpl.SERVICE_TIMEOUT == iReturnCode) {
            rglog.error("调用银联cups服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            cupsLineTestResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            cupsLineTestResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            chnlBizResponse.setResult(cupsLineTestResponse);
            return chnlBizResponse;
        } else if (0 != iReturnCode) {
            rglog.error("调用银联cups服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }

        CupsFieldRspBean cupsFieldRspBean = new CupsFieldRspBean();
        ConvertUtil.convertOutput(cupsFieldRspBean);
        CupsField cupsFieldRsp = cupsFieldRspBean.getCupsFieldRsp();
        BeanUtil.beanCopy(cupsFieldRsp, cupsLineTestResponse);
        cupsLineTestResponse.setRespCode(cupsFieldRsp.getRespCode());

        if (CupsParamConstant.SUCCESS_CODE.equals(cupsLineTestResponse.getRespCode())) {
            cupsLineTestResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            cupsLineTestResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
        }
        chnlBizResponse.setResult(cupsLineTestResponse);
        return chnlBizResponse;
    }

}
